(function() {
  var debug, http, https, on_server_error, on_server_listening, server, webapp;

  debug = require('debug')('luckpay-web:server');

  http = require('http');

  https = require('https');

  webapp = require('./webapp');

  on_server_error = function(error) {
    if (error.syscall !== 'listen') {
      throw error;
    }
    switch (error.code) {
      case 'EACCES':
        console.error('绑定端口 ' + settings.listen_port + ' 需要特殊权限.');
        return process.exit(1);
      case 'EADDRINUSE':
        console.error('端口 ' + settings.listen_port + ' 已被占用.');
        return process.exit(1);
      default:
        throw error;
    }
  };

  on_server_listening = function() {
    return debug('监听端口 ' + server.address().port);
  };

  server = http.createServer(webapp);

  server.listen(settings.listen_port, settings.listen_addr);

  server.on('error', on_server_error);

  server.on('listening', on_server_listening);

}).call(this);
